﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using System.Linq;
using System.Data.Linq;
namespace Exact_Extension
{
    public partial class MsgEditStudent : DevExpress.XtraEditors.XtraForm
    {
        ExactDataContext DS;
        ISHCMC_V_StudentListWithDebtor RootItemStudent;
        public MsgEditStudent(ExactDataContext _DS)
        {
            InitializeComponent();
            DS = _DS;
            this.LoadData();
        }
        private void LoadData()
        {
            var querySchool = from School in DS.ISHCMC_V_Schools
                              select School;
            this.iSHCMCVSchoolBindingSource.DataSource = querySchool;

            var querySchoolYear = from SchoolYear in DS.ISHCMC_SchoolYears
                                  select SchoolYear;
            this.iSHCMCSchoolYearBindingSource.DataSource = querySchoolYear;

            var queryDivison = from Division in DS.ISHCMC_Divisions
                               select Division;

            this.iSHCMCDivisionBindingSource.DataSource = queryDivison;

            var queryGrade = from Grade in DS.ISHCMC_Grades
                             select Grade;
            //List<ISHCMC_Grade> ListGrade = new List<ISHCMC_Grade>();
            //foreach (ISHCMC_Grade itemGrade in queryGrade)
            //{
            //    if (itemGrade.GradeCode.StartsWith("E"))
            //    {
            //        itemGrade.GradeCode = itemGrade.GradeCode.Substring(1, 2);
            //    }
            //    ListGrade.Add(itemGrade);
            //}
            this.iSHCMCGradeBindingSource.DataSource = queryGrade;

            var queryPM = from PaymentMethod in DS.ISHCMC_PaymentMethods
                          select PaymentMethod;
            this.iSHCMCPaymentMethodBindingSource.DataSource = queryPM;

        }
        public void PopulateControl(ISHCMC_V_StudentListWithDebtor itemStudent)
        {

            RootItemStudent = itemStudent;

            this.txtDebtorcode.Text = itemStudent.DebtorCode;
            this.txtStudentID.Text = itemStudent.StudentID;
            this.txtStudentName.Text = itemStudent.StudentName;
            this.txtAddress1.Text = itemStudent.Address1;
            this.txtAddress2.Text = itemStudent.Address2;
            this.txtAddress3.Text = itemStudent.Address3;
            if(itemStudent.School !=  null)
            this.cbxSchool.EditValue = itemStudent.School.ToString();
            if(itemStudent.Division !=null)
            this.cbxDivision.EditValue = itemStudent.Division.ToString();
            if(itemStudent.Grade != null)
            this.cbxGrade.EditValue = itemStudent.Grade.ToString();
            
            if (itemStudent.HalfDay == 1)
            {
                this.chkHalfday.Checked = true;
            }
            else
            {
                this.chkHalfday.Checked = false;
            }

            if (itemStudent.SchoolYear != null)
            {
                this.cbxSchoolYear.EditValue = itemStudent.SchoolYear.ToString();
            }

            if (itemStudent.PaymentMethod != null)
            {
                this.cbxPaymentMethod.EditValue = itemStudent.PaymentMethod;
            }
            else
            {
                this.cbxPaymentMethod.EditValue = "A";
            }
            this.txtBillingAddress1.Text = itemStudent.BillingAddress1;
            this.txtBillingAddress2.Text = itemStudent.BillingAddress2;
            this.txtBillingAddress3.Text = itemStudent.BillingAddress3;
            this.txtEmailBilling.Text = itemStudent.EmailBilling;
            this.txtEmail1.Text = itemStudent.Email1;
            this.txtEmail2.Text = itemStudent.Email2;

            if (itemStudent.COE == 1)
            {
                this.chkCOEStatus.Checked = true;
                this.txtCOENumber.Text = itemStudent.COENumber.ToString();
            }
            else
            {
                this.chkCOEStatus.Checked = false;
                this.txtCOENumber.Text = "0";

            }
            if (itemStudent.EnrollmentDate.HasValue)
            {

                this.dateEdit_Enrollment.EditValue = itemStudent.EnrollmentDate.Value;
            }
            this.txtAttention.Text = itemStudent.Attention;
            if(itemStudent.NoticeLeavingDate.HasValue)
            {
                this.dateEdit_NoticeLeaving.EditValue = itemStudent.NoticeLeavingDate.Value;
            }
            if (itemStudent.ActualWithdrawDate.HasValue)
            {
                this.dateEdit_WithdrawnDate.EditValue = itemStudent.ActualWithdrawDate.Value;
            }
        }

     

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.Cancel;
            this.Close();
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            try
            {
                cicmpy studentUpdate = DS.cicmpies.Single(c => c.textfield4 == RootItemStudent.StudentID);


                studentUpdate.cmp_name = this.txtStudentName.Text;
                studentUpdate.cmp_fadd1 = this.txtAddress1.Text;
                studentUpdate.cmp_fadd2 = this.txtAddress2.Text;
                studentUpdate.cmp_fadd3 = this.txtAddress3.Text;
                if (this.cbxSchool.EditValue != null)
                    studentUpdate.textfield1 = this.cbxSchool.EditValue.ToString();
                if (this.cbxDivision.EditValue != null)
                    studentUpdate.textfield2 = this.cbxDivision.EditValue.ToString();
                if (this.cbxGrade.EditValue != null)
                    studentUpdate.textfield3 = this.cbxGrade.EditValue.ToString();
                if (chkHalfday.Checked)
                {
                    studentUpdate.YesNofield2 = 1;
                }
                else
                {
                    studentUpdate.YesNofield2 = 0;
                }

                if (this.cbxSchoolYear.EditValue != null)
                {
                    studentUpdate.textfield5 = this.cbxSchoolYear.EditValue.ToString();
                }

                if (this.cbxPaymentMethod.EditValue != null)
                {
                    studentUpdate.TextField6 = this.cbxPaymentMethod.EditValue.ToString();
                }
                studentUpdate.TextField7 = this.txtBillingAddress1.Text;
                studentUpdate.TextField8 = this.txtBillingAddress2.Text;
                studentUpdate.TextField9 = this.txtBillingAddress3.Text;

                studentUpdate.TextField10 = this.txtEmail1.Text;
                studentUpdate.TextField11 = this.txtEmail2.Text;
                studentUpdate.TextField12 = this.txtEmailBilling.Text;
                studentUpdate.TextField13 = this.txtAttention.Text;
                if (chkCOEStatus.Checked)
                {
                    studentUpdate.YesNofield1 = 1;
                    studentUpdate.numberfield1 = Convert.ToDouble(this.txtCOENumber.Text);
                }
                else
                {
                    studentUpdate.YesNofield1 = 0;
                    studentUpdate.numberfield1 = 0;
                }
                if (this.dateEdit_WithdrawnDate.EditValue != null)
                {
                    studentUpdate.datefield4 = Convert.ToDateTime(this.dateEdit_WithdrawnDate.EditValue);

                }
                if (this.dateEdit_NoticeLeaving.EditValue != null)
                {
                    studentUpdate.datefield3 = Convert.ToDateTime(this.dateEdit_NoticeLeaving.EditValue);
                }
                if (this.dateEdit_Enrollment.EditValue != null)
                {
                    studentUpdate.datefield1 = Convert.ToDateTime(this.dateEdit_Enrollment.EditValue);
                }
                DS.SubmitChanges();

                var queryAddress = from Add in DS.Addresses
                                   where Add.Account == studentUpdate.cmp_wwn
                                   select Add;
                foreach (Address itemAdd in queryAddress)
                {
                    Address AddUpdate = DS.Addresses.Single(c => c.ID == itemAdd.ID);
                    AddUpdate.AddressLine1 = studentUpdate.cmp_fadd1;
                    AddUpdate.AddressLine2 = studentUpdate.cmp_fadd2;
                    AddUpdate.AddressLine3 = studentUpdate.cmp_fadd3;
                    DS.SubmitChanges();
                }
                this.DialogResult = DialogResult.OK;
                this.Close();
            }
            catch (Exception ex)
            {
                this.DialogResult = DialogResult.No;
                this.Close();
            }

        }
    }
}